package com.baichuan.service.stickyNote;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baichuan.entity.stickyNote.StickyNote;
import com.baichuan.mapper.stickyNote.StickyNoteMapper;
import com.baichuan.mapper.stickyNote.StickyNoteUpdateLogMapper;
import com.baichuan.util.ResponseResult;
import com.baichuan.util.StringUtils;
import com.baichuan.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author ldy
 * @Date 2022/8/2 9:28
 * @Description
 **/
@Service
public class StickyNoteService {

    @Resource
    StickyNoteMapper stickyNoteMapper;
    @Resource
    StickyNoteUpdateLogMapper stickyNoteUpdateLogMapper;
    /**
     * 新增便利贴
     * @param params
     * @return
     */
    public String insert(JSONObject params) {
        StickyNote stickyNote = params.toJavaObject(StickyNote.class);
        if (StringUtils.isEmpty(stickyNote.getTitle()) && StringUtils.isEmpty(stickyNote.getContent()))
            return ResponseResult.writeInvalid("标题和内容不能同时为空");

        if (!StringUtils.isEmpty(stickyNote.getIsAgency()) && StringUtils.isEmpty(stickyNote.getAgencyStartTime(),stickyNote.getAgencyEndTime()))
            return ResponseResult.writeInvalid("待办时间不能为空");

        stickyNote.setStickyNoteId(UuidUtil.getUuid());
        stickyNote.setLogId(UuidUtil.getUuid());

        stickyNote.setCreateTime(new Date());

        stickyNoteMapper.insert(stickyNote);
        stickyNoteUpdateLogMapper.insert(stickyNote);
        return ResponseResult.writeSuccess("result",stickyNote.getStickyNoteId());
    }

    /**
     * 修改便利贴
     * @param params
     * @return
     */
    public String update(JSONObject params) {
        StickyNote stickyNote = params.toJavaObject(StickyNote.class);
        if (StringUtils.isEmpty(stickyNote.getStickyNoteId()))
            return ResponseResult.writeInvalid("便利贴id为空,请刷新后重试");

        if (!StringUtils.isEmpty(stickyNote.getIsAgency()) && StringUtils.isEmpty(stickyNote.getAgencyStartTime(),stickyNote.getAgencyEndTime()))
            return ResponseResult.writeInvalid("待办时间不能为空");

        if (StringUtils.isEmpty(stickyNote.getTitle()) && StringUtils.isEmpty(stickyNote.getContent()))
            return ResponseResult.writeInvalid("标题和内容不能同时为空");

        stickyNote.setLogId(UuidUtil.getUuid());
        stickyNote.setUpdateTime(new Date());
        stickyNote.setCreateTime(new Date());
        stickyNoteMapper.update(stickyNote);
        stickyNoteUpdateLogMapper.insert(stickyNote);
        return ResponseResult.writeSuccess("result",stickyNote.getStickyNoteId());
    }

    /**
     * 删除便利贴
     * @param params
     * @return
     */
    public String delete(JSONObject params) {
        String stickyNoteId = params.getString("stickyNoteId");
        if (StringUtils.isEmpty(stickyNoteId))
            return ResponseResult.writeInvalid("便利贴id为空,请刷新后重试");
        StickyNote stickyNote=stickyNoteMapper.findByStickyNoteId(stickyNoteId);
        if (StringUtils.isEmpty(stickyNote))
            return ResponseResult.writeInvalid("便利贴不存在,请刷新后重试");
        stickyNote.setLogId(UuidUtil.getUuid());
        stickyNote.setUpdateTime(new Date());
        stickyNote.setCreateTime(new Date());
        stickyNote.setDeleteTime(new Date());
        stickyNoteUpdateLogMapper.insert(stickyNote);
        stickyNoteMapper.updateToDelete(stickyNoteId);
        return ResponseResult.writeSuccess("result",stickyNoteId);
    }

    public String list(JSONObject params) {
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");
        if (StringUtils.isEmpty(pageNum, pageSize))
            return ResponseResult.writeInvalid();
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> queryMap = params.toJavaObject(new TypeReference<Map<String, Object>>() {
        });

        List<StickyNote> list=stickyNoteMapper.list(queryMap);
        PageInfo pageInfo = new PageInfo(list);

        JSONObject result = new JSONObject();
        result.put("posterList", list);
        result.put("total", pageInfo.getTotal());
        result.put("pages", pageInfo.getPages());
        return ResponseResult.writeSuccess("result", result);
    }
}
